package core

import (
    "net/http"
    "reflect"
    "strings"
    "gougouapi/core/render"
    "log"
)

type Engine struct {

}

func NewEngine() *Engine {
    e := new(Engine)
    return e
}

func (this *Engine) Register(handler HttpHandlerInterface) error {
    reg := NewRegister()
    return reg.Reg(handler)
}

func (this *Engine) HttpRun(conf *HttpConf) error {
    if conf.JsonRender == nil {
        conf.JsonRender = &render.Json{}
    }
    if conf.HTMLRender == nil {
        //conf.HTMLRender = render.NewHTMLProduction([]string{}, "", nil, true)
    }
    if conf.DefaultReplyType == 0 {
        conf.DefaultReplyType = ReplyTypeJSON
    }
    handler := NewHttp()
    handler.Conf = conf
    handler.HandlerMap = map[string]reflect.Type{}
    reg := NewRegister()
    for _, h := range reg.HandlerContainer {
        typ := reflect.TypeOf(h)
        key := typ.String()
        tmpAry := strings.Split(key, ".")
        key = tmpAry[len(tmpAry) - 1]
        handler.HandlerMap[key] = typ
    }
    log.Fatalln(http.ListenAndServe(conf.Addr, handler))
    return nil
}
